package com.example.mybatisplus.identifier.generator;

import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.core.toolkit.Sequence;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 当有@param注解标记sql语句，如果不是@param("et")，则不会为其参数自动生成主键。
 * 注意：@param标记，一定要@param("et")
 */
@Component
public class CustomIdentifierGenerator implements IdentifierGenerator {

    private final Sequence sequence;
    public CustomIdentifierGenerator(){
        this.sequence = new Sequence(null);
    }
    public CustomIdentifierGenerator(InetAddress inetAddress) {
        this.sequence = new Sequence(inetAddress);
    }

    public CustomIdentifierGenerator(long workerId, long dataCenterId) {
        this.sequence = new Sequence(workerId, dataCenterId);
    }
    public CustomIdentifierGenerator(Sequence sequence) {
        this.sequence = sequence;
    }
    @Override
    public boolean assignId(Object idValue) {
        System.out.println("==============================");
        System.out.println(IdentifierGenerator.super.assignId(idValue));
        System.out.println(idValue);
        //如果主键值为null，则返回true，代表分配id值，否则返回false
        return true;
    }

    @Override
    public String nextUUID(Object entity) {
        return IdentifierGenerator.super.nextUUID(entity);
    }

    @Override
    public Long nextId(Object entity) {
        return sequence.nextId();
    }
}
